package engine;

import java.awt.Dimension;
import reflection.Reflection;
import generics.AbstractGroup;
import gui.Canvas;
import gui.ResourceStrings;

/**
 * This class is created groups of type passed in through the constructor.
 * Reflectively creates correct type of group associated with the instance of
 * factory created.
 * 
 * @author jianweigan
 */
public class Factory {

	private String myName;
	private Integer myNumCreated;
	private String myFullGroupClassName;
	private AbstractGroup myNewestCreated;

	/**
	 * Creates a Factory that can create groups of the class passed to it.
	 * 
	 * @param groupClass
	 *            class of groups to be created by this factory
	 */
	public Factory(Class<?> groupClass) {
		myName = ResourceStrings.getString("FactoryAddNew")
				+ groupClass.getSimpleName();
		myFullGroupClassName = groupClass.getName();
		myNumCreated = 0;
	}

	/**
	 * Reports the name of this factory (displayed on the button used to
	 * activate it)
	 * 
	 * @return name of this factory
	 */
	public String getName() {
		return myName;
	}

	/**
	 * @return number of groups that have been created by this factory
	 */
	public int getNumCreated() {
		return myNumCreated;
	}

	/**
	 * Subclasses determine how to create the movers and add them to the canvas
	 * increments num created by 1 uses reflection to create the group of type
	 * associated to this factory
	 * 
	 * @param canvas
	 *            that will display created movers
	 * @param number
	 *            number of movers to create
	 * @param trailSize
	 *            number of steps to draw in the trail
	 */
	public void createGroup(Canvas canvas) {
		myNumCreated++;
		Dimension d = canvas.getSize();
		AbstractGroup group = (AbstractGroup) Reflection.createInstance(
				myFullGroupClassName, d, myNumCreated);
		canvas.add(group);
		myNewestCreated = group;
	}

	/**
	 * 
	 * @return newest group created by this factory or null if none have been
	 *         created yet.
	 */
	public AbstractGroup getNewestGroup() {
		if (myNumCreated > 0)
			return myNewestCreated;
		return null;
	}
}
